<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Members
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class GroupsController extends Controller
{
	function add()
	{
		$this->Group->id = null;
		
		if (!$this->acl->inGroup(Config::admins))
			$this->acl->permissionDenied();
		
		if ($this->Group->save())
		{
			$member_id = $this->data['Group']['member_id'];
			$email     = $this->Group->Member->field('email', $member_id, -1);
			$group     = $this->acl->groupTable[$this->data['Group']['name']];
			$this->set('group', $group);
			
			// informs member
			$this->load->components('swift');
			$this->swift->addRecipient($email, $member_id);
			$this->swift->send(s_("You've been added to group %s", $group),
				array(Config::website_email, Config::website_name));
			
			$this->session->setFlash(s_('Member has been added to group %s.', $group));
		}
		else {
			$this->session->setFlash(_('Oops, an error occured.'));
		}
		
		$this->redirect('/members?q='.urlencode($this->data['Group']['member_id']));
	}
	
	function remove($member_id, $name)
	{
		$this->Group->conditions(array('member_id' => $member_id, 'name' => $name));
		$this->Group->id = $this->Group->field('id', null, -1);
		
		if ($this->Group->id and $this->Group->delete())
		{
			$email = $this->Group->Member->field('email', $member_id, -1);
			$group = $this->acl->groupTable[$name];
			$this->data['Group'] = array('member_id' => $member_id, 'group' => $group);
			
			// informs member
			$this->load->components('swift');
			$this->swift->addRecipient($email, $member_id);
			$this->swift->send(s_("You've been removed from group %s", $group),
				array(Config::website_email, Config::website_name));
			
			$this->session->setFlash(s_('Member has been removed from group %s.', $group));
		}
		else {
			$this->session->setFlash(_('Oops, an error occured.'));
		}
		
		$this->redirect('/members?q='.urlencode($member_id));
	}
}
?>